package com.imooc.dianping.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.dianping.request.PageInfo;
import com.imooc.dianping.dao.SellerMapper;
import com.imooc.dianping.model.Seller;
import com.imooc.dianping.request.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class SellerService extends ServiceImpl<SellerMapper, Seller> {

    @Autowired
    private SellerMapper sellerMapper;

    @Transactional(rollbackFor = Exception.class)
    public void create(Seller seller){
        save(seller);
    }

    @Transactional(readOnly = true)
    public Seller get(Integer id){
        return sellerMapper.selectById(id);
    }

    @Transactional(readOnly = true)
    public PageInfo<Seller> selectAll(PageQuery pageQuery){
        Page<Seller> page = new Page<>(pageQuery.getPage(),pageQuery.getSize());
        Page<Seller> result = sellerMapper.selectPageOrderById(page);
        PageInfo<Seller> pageInfo = new PageInfo<>();
        pageInfo.setList(result.getRecords());
        pageInfo.setTotal(result.getTotal());
        pageInfo.setPageNum(result.getCurrent());
        pageInfo.setPageSize(result.getSize());
        pageInfo.setPages(result.getPages());
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    public Seller changeStatus(Integer id,Integer disableFlag){
        Seller seller = new Seller();
        seller.setId(id);
        seller.setDisabledFlag(disableFlag);
        updateById(seller);
        return sellerMapper.selectById(id);
    }

}
